package com.itcam.cm.cart.controller;


import com.alibaba.fastjson.JSON;
import com.itcam.cm.cart.service.CartService;
import com.itcam.cm.model.entity.cart.CartInfo;
import com.itcam.cm.model.entity.user.UserInfo;
import com.itcam.cm.model.vo.common.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @GetMapping("/auth/getAllChecked")
    List<CartInfo> getAllChecked() {
        return cartService.getAllChecked();
    }


    @GetMapping("/auth/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId, @PathVariable Integer skuNum, HttpServletRequest request){
        String userJson = request.getHeader("userJson");
        UserInfo userInfo = JSON.parseObject(userJson, UserInfo.class);
        cartService.addToCart(skuId,skuNum);
        return Result.ok(null);
    }

    @GetMapping("/auth/cartList")
    public Result<List<CartInfo>> cartList(){
        List<CartInfo> cartInfos = cartService.cartList();
        return Result.ok(cartInfos);
    }

    @DeleteMapping("/auth/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId){
        cartService.deleteCart(skuId);
        return Result.ok(null);
    }

    @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,@PathVariable Integer isChecked){
        cartService.checkCart(skuId,isChecked);
        return Result.ok(null);
    }

    @GetMapping("auth/deleteCheckedCart")
    Result deleteCheckedCart(){
        cartService.deleteCheckedCart();
        return Result.ok(null);
    }

}
